package com.ctgu.springshiro.dao;

import com.ctgu.springshiro.entity.ItemKillSuccess;
import com.ctgu.springshiro.entity.KillSuccessUserInfo;
import com.ctgu.springshiro.utils.DefineSqlProvider;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ItemKillSuccessDao {
    int deleteByPrimaryKey(String code);

    int insert(ItemKillSuccess record);

    @InsertProvider(type = DefineSqlProvider.class, method = "insertSelective")
    int insertSelective(ItemKillSuccess record);

    @Select("select code, item_id, kill_id, user_id, " +
            "   status, create_time from item_kill_success where code = #{code}")
    @Results({
            @Result(property = "code", column = "code"),
            @Result(property = "itemId", column = "item_id"),
            @Result(property = "killId", column = "kill_id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "status", column = "status"),
            @Result(property = "createTime", column = "create_time")
    })
    ItemKillSuccess selectByPrimaryKey(String code);

    int updateByPrimaryKeySelective(ItemKillSuccess record);

    int updateByPrimaryKey(ItemKillSuccess record);

    @Select("SELECT COUNT(1) AS total FROM item_kill_success " +
            "   WHERE user_id = #{userId} AND kill_id = #{killId} AND `status` IN (0)")
    int countByKillUserId(@Param("killId") Integer killId, @Param("userId") Integer userId);

    @Select("SELECT" +
//            "      a.*," +
            "      a.code AS code," +
            "      b.username AS username," +
            "      b.phone AS phone," +
            "      b.email AS email," +
            "      c.name AS itemName," +
            "      a.create_time AS createTime" +
            "    FROM item_kill_success AS a" +
            "      LEFT JOIN user b ON b.id = a.user_id" +
            "      LEFT JOIN item c ON c.id = a.item_id" +
            "    WHERE a.code = #{code}" +
            "          AND b.is_active = 1")
//    @Results({
//            @Result(property = "code", column = "code"),
//            @Result(property = "userName", column = "username"),
//            @Result(property = "phone", column = "phone"),
//            @Result(property = "email", column = "email"),
//            @Result(property = "itemName", column = "itemName")
//    })
    KillSuccessUserInfo selectByCode(@Param("code") String code);

    @Update("UPDATE item_kill_success" +
            "    SET status = -1" +
            "    WHERE code = #{code} AND status = 0")
    int expireOrder(@Param("code") String code);

    @Select("SELECT" +
            "        a.*,TIMESTAMPDIFF(MINUTE,a.create_time,NOW()) AS diffTime" +
            "    FROM" +
            "        item_kill_success AS a" +
            "    WHERE" +
            "        a.`status` = 0")
    List<ItemKillSuccess> selectExpireOrders();
}
